var globalGroupRoomObject;
var globalOwnedRooms;
var globalOtherRooms;

function startTasks(){
	globalGroupRoomObject = JSON.parse(globalGroupRoom);

	var ownedRoomsList = document.getElementById('ownedGroups').getElementsByClassName('list')[0];

	var otherRoomsList = document.getElementById('otherGroups').getElementsByClassName('list')[0];

	var ownedGroups = globalGroupRoomObject.ownedGroups;
	var groups = globalGroupRoomObject.groups;
	var groupAsRooms = globalGroupRoomObject.openedGroupsAsRooms;
	var roomSessions = globalGroupRoomObject.roomSessions;

	var currentGroup,room,obj,key,session;
	for(var key in groups){
		currentGroup = groups[key];
		session = room = false;

		if('group_'+currentGroup.id in groupAsRooms){
			room = groupAsRooms['group_'+currentGroup.id];
			if(room in roomSessions) session = roomSessions[room];
		}

		obj = {id: currentGroup.id, name: currentGroup.name, room: room, session: session};
		key = 'group_'+obj.id;
		
		if('administrator' in currentGroup){
			//globalOwnedRooms[key] = obj;
			obj.administrator = true;
			ownedRoomsList.appendChild(createGroupElement(obj));
		}else if(room != false){
			//globalOtherRooms[key] = obj;
			otherRoomsList.appendChild(createGroupElement(obj));
		}
	}
}

function createGroupElement(obj){
	var el = document.createElement('DIV');
	el.className = 'group item';
	
	setData(el,'group',obj.id);
	if(obj.room) setData(el,'room',obj.room.split('_')[1]);
	if(obj.session) setData(el,'session',obj.session);

	var name = document.createElement('DIV');
	name.className = 'name';
	var maxNameLen = (obj.session) ? 25 : 35;
	name.innerHTML = (obj.name.length > maxNameLen) ? obj.name.substr(0,maxNameLen)+'...' : obj.name;

	el.appendChild(name);

	var itemClass = itemText = '';

	if(obj.administrator){

		var openCloseButton = document.createElement('DIV');
		openCloseButton.className = 'button';

		var icon = document.createElement('DIV');
		icon.className = 'icon';

		var buttonName = document.createElement('DIV');
		buttonName.className = 'name';

		openCloseButton.appendChild(buttonName);
		openCloseButton.appendChild(icon);

		if(obj.room){

			buttonName.innerHTML = 'Fechar';

			openCloseButton.className += ' close';
			openCloseButton.title = 'Fechar sala';

			var enterLeaveButton = document.createElement('DIV');
			enterLeaveButton.className = 'button';

			var name2 = document.createElement('DIV');
			name2.className = 'name';

			if(!obj.session){
				itemClass = 'startSession';
				enterLeaveButton.className += ' enter';
				enterLeaveButton.title = 'Entrar na sala';

				setEvent(enterLeaveButton,'click',enterRoom);

				name2.innerHTML = 'Entrar';
			}else{
				itemClass = 'sessionStarted';
				itemText = '(sessão iniciada)';
				enterLeaveButton.title = 'Sair da sala';
				enterLeaveButton.className += ' leave';

				setEvent(enterLeaveButton,'click',leaveRoom);

				name2.innerHTML = 'Sair';
			}

			var icon2 = document.createElement('DIV');
			icon2.className = 'icon';

			enterLeaveButton.appendChild(name2);
			enterLeaveButton.appendChild(icon2);

			setEvent(openCloseButton,'click',closeRoom);		

		}else{
			buttonName.innerHTML = 'Abrir';

			openCloseButton.title = 'Abrir sala';
			openCloseButton.className += ' open';

			setEvent(openCloseButton,'click',openRoom);
		}

		el.appendChild(openCloseButton);
		if(enterLeaveButton) el.appendChild(enterLeaveButton);

	}else{

		var enterLeaveButton = document.createElement('DIV');
		enterLeaveButton.className = 'button';

		var buttonName = document.createElement('DIV');
		buttonName.className = 'name';
		
		if(!obj.session){
			itemClass = 'startSession';
			enterLeaveButton.title = 'Entrar na sala';
			enterLeaveButton.className += ' enter';

			buttonName.innerHTML = 'Entrar';

			setEvent(enterLeaveButton,'click',enterRoom);
		}else{
			itemClass = 'sessionStarted';
			itemText = '(sessão iniciada)';
			enterLeaveButton.title = 'Sair da sala';
			enterLeaveButton.className += ' leave';

			buttonName.innerHTML = 'Sair';

			setEvent(enterLeaveButton,'click',leaveRoom);
		}

		var icon = document.createElement('DIV');
		icon.className = 'icon';

		enterLeaveButton.appendChild(buttonName);
		enterLeaveButton.appendChild(icon);

		el.appendChild(enterLeaveButton);

	}	

	el.className += ' '+itemClass;
	name.innerHTML += ' '+itemText;

	return el;
}